<template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" class="ant-form ant-form-horizontal">
        <a-card  id="staffCard" class="ant-card ant-card-bordered" style="margin: 0px auto; width: 1000px;">
          <span id="staffLeaveTitle">公文发文</span>
          <table border="1px" id="staffEvectionTable">
            <tr>
              <td><span>机关待字：</span></td>
              <td>
                <a-form-item>
                  <j-dict-select-tag
                    v-decorator="['organId',validatorRules.organId]"
                    :trigger-change="true"
                    dictCode="oa_officialdoc_organcode,organ_name,id"
                    placeholder="请选择机关代字"
                    @change="codeChange"
                    :disabled="true"/>
                </a-form-item>
              </td>
              <td>
                <span>文号：</span>
              </td>
              <td>
                <a-form-item>
                  <a-input v-decorator="['code']" placeholder="请输入文号"  class="inputHeight" :disabled="true"/>
                </a-form-item>
              </td>
            </tr>
            <tr>
              <td>
                <span>缓急：</span>
              </td>
              <td>
                <a-form-item>
                  <j-dict-select-tag
                    type="list" v-decorator="['urgency']"
                    :trigger-change="true"
                    dictCode="urgency"
                    placeholder="请选择缓急"
                    style="width:116px"
                    :disabled="true"/>
                </a-form-item>
              </td>
              <td>
                <span>密级：</span>
              </td>
              <td>
                <a-form-item>
                  <j-dict-select-tag
                    type="list"
                    v-decorator="['secretLevel']"
                    :trigger-change="true"
                    dictCode="secret_level"
                    placeholder="请选择密级"
                    style="width:116px"
                    :disabled="true"/>
                </a-form-item>
              </td>
            </tr>
          <tr class="trPadding">
            <td style="text-align: center"><span>部门会签：</span></td>
            <td colspan="3">
              <a-form-item>
                <a-textarea v-decorator="['jointlySigns']"  class="textType" :disabled="isDisabledAuth('issued:jointly')"/>
              </a-form-item>
            </td>
          </tr>
            <tr class="trPadding">
              <td><span style="text-align: center">领导会签：</span></td>
              <td colspan="3">
                <a-form-item>
                  <a-textarea v-decorator="['leaderJointlySigns']"  class="textType" :disabled="isDisabledAuth('issued:leader')"/>
                </a-form-item>
              </td>
            </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>拟稿人：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <j-select-user-by-dep v-decorator="['draftUser']"  :multi="false" :disabled="true"/>
              </a-form-item>
            </td>
          </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>拟稿部门：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <j-select-depart v-decorator="['draftDept']" :customReturnField="customReturnField" disabled/>
              </a-form-item>
            </td>
          </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>拟稿人意见：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <a-textarea v-decorator="['draftUserOpinion']"  class="textType" read-only/>
              </a-form-item>
            </td>
          </tr>
            <tr class="trPadding">
              <td>
                <span>办公室核稿意见：</span>
              </td>
              <td colspan="3">
                <a-form-item>
                  <a-textarea v-decorator="['reviewUserOpinion']" class="textType" :disabled="isDisabledAuth('issued:draft')"/>
                </a-form-item>
              </td>

            </tr>
            <tr class="trPadding">
              <td>
                <span>办公室领导核稿意见：</span>
              </td>
              <td colspan="3">
                <a-form-item>
                  <a-textarea v-decorator="['officeLeadersOption']" class="textType"  :disabled="isDisabledAuth('issued:office:leaders')"/>
                </a-form-item>
              </td>
            </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>校对意见：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <a-textarea v-decorator="['proofreadUser']"  class="textType" :disabled="isDisabledAuth('issued:proofreading')" />
              </a-form-item>
            </td>
          </tr>
            <tr class="trPadding">
              <td>
                <span>主办单位审核：</span>
              </td>
              <td colspan="3">
                <a-form-item>
                  <a-textarea v-decorator="['sponsorReview']"  class="textType" :disabled="isDisabledAuth('issued:sponsor')"/>
                </a-form-item>
              </td>
            </tr>
            <tr class="trPadding">
              <td>
                <span>秘书审核：</span>
              </td>
              <td colspan="3">
                <a-form-item>
                  <a-textarea v-decorator="['secretaryReview']"  class="textType" :disabled="isDisabledAuth('issued:secretary')"/>
                </a-form-item>
              </td>
            </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>打印份数：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <a-input-number v-decorator="['printingNumber']" class="inputHeight" disabled/>
              </a-form-item>
            </td>
          </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>标题：</span>
            </td>
            <td colspan="3" class="title">
              <a-form-item>
                <a-input v-decorator="['title',validatorRules.title]" placeholder="请输入标题"  read-only/>
              </a-form-item>
            </td>
          </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>主送：</span>
            </td>
            <td colspan="3" class="title">
              <a-form-item>
                <j-select-depart v-decorator="['mainDelivery',validatorRules.mainDelivery]" :disabled="true"/>
              </a-form-item>
            </td>
          </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>抄送：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <j-select-depart v-decorator="['ccDelivery']" :disabled="true"/>
              </a-form-item>

            </td>
          </tr>
          <tr class="trPadding">
            <td style="text-align: center">
              <span>附件：</span>
            </td>
            <td colspan="3">
              <a-form-item>
                <j-upload :bizPath="filePath" v-model="fileUrl" text="附件上传" :disabled="true"></j-upload>
              </a-form-item>

            </td>
          </tr>
        </table>
     </a-card>
        <a-col :span="24" style="text-align: center;margin-top: 20px">
          <a-button @click="handleOk" key="submit" type="primary">提 交</a-button>
          <a-button key="draftText" type="primary" @click="draftTextClick" style="margin-left: 20px" :disabled="isDisabledAuth('issued:typesetting')">排 版</a-button>
          <a-button key="signature" type="primary" @click="signature" style="margin-left: 20px" :disabled="isDisabledAuth('issued:signature')">签 章</a-button>
          <a-button key="red" type="primary" @click="draftTextClick" style="margin-left: 20px" :disabled="isDisabledAuth('issued:red')">套 红</a-button>
        </a-col>
      </a-form>
    </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
  import JUpload from '@/components/jeecg/JUpload'
  import store from '@/store'
  import { disabledAuthFilter } from "@/utils/authFilter"

  export default {
    name: 'OaOfficialdocIssuedForm',
    components: {
      JDate,
      JSelectDepart,
      JSelectUserByDep,
      JDictSelectTag,
      JMultiSelectTag,
      JUpload
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      normal: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 1000,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        sendLoading: false,
        draftLoading: false,
        loading: false,
        validatorRules: {
          organId: {
            rules: [
              { required: true, message: '请选择机关代字!' }
            ]
          },
          title: {
            rules: [
              { required: true, message: '请填写标题!' }
            ]
          },
          mainDelivery: {
            rules: [
              { required: true, message: '请选择主送!' }
            ]
          },
        },
        url: {
          add: '/eoa/officialdoc/oaOfficialdocIssued/add',
          edit: '/eoa/officialdoc/oaOfficialdocIssued/edit',
          getCode: '/eoa/officialdoc/oaOfficialdocIssued/getCode',
          getDepartName: '/sys/sysDepart/getDepartName',
          sendText: '/eoa/officialdoc/oaOfficialdocIssued/sendText',
          getTemplateAndUser: '/eoa/officialdoc/oaOfficialdocOrgancode/getTemplateAndUser',
          getViewUrlDbPath: '/eoa/wps/oaWpsFile/getViewUrlDbPath',
          officialTemp:"/eoa/officialdoc/oaOfficialdocOrgancode/officialTemp",
          getDepartId:"/eoa/officialdoc/oaOfficialdocIssued/getDepartId",
          queryById:"/eoa/officialdoc/oaOfficialdocIssued/queryById",
          getSignature:"/eoa/officialdoc/oaOfficialdocIssued/getSignature",
        },
        fileUrl: [],
        draftText: [],
        filePath: 'enclosure',
        DrafTextPath: 'draftText',
        departName: '',
        text: false,
        draft:false,
        customReturnField:"id",
      }
    },
    created() {
      this.showFlowData();
    },
    methods: {
      add() {
        this.edit({})
      },
      showFlowData(){
        if(this.normal === false){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.fileUrl = this.model.fileUrl
        this.draftText = this.model.draftText
        // let draftDept = this.model.draftDept
        console.log(record)
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'title', 'mainDelivery', 'ccDelivery', 'organId', 'code', 'urgency', 'secretLevel', 'issuedUser','draftUser', 'draftDept','jointlySigns', 'reviewUser', 'draftUserOpinion', 'reviewUserOpinion', 'proofreadUser', 'printingNumber','sponsorReview','secretaryReview','officeLeadersOption','leaderJointlySigns'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            formData.fileUrl = this.fileUrl
            formData.draftText = this.draftText
            formData.stauts = '1'
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'title', 'mainDelivery', 'ccDelivery', 'organId', 'code', 'urgency', 'secretLevel', 'issuedUser', 'jointlySigns', 'draftDept', 'draftUser', 'reviewUser', 'draftUserOpinion', 'reviewUserOpinion', 'proofreadUser', 'printingNumber', 'fileUrl', 'stauts', 'bpmStatus', 'createBy', 'createTime', 'updateBy', 'updateTime', 'sysOrgCode', 'draftText'))
      },
      codeChange(value) {
        getAction(this.url.getCode, { organId: value }).then((res) => {
          if (res.success) {
            this.form.setFieldsValue({ 'code': res.result })
          }
        })
      },
      draftTextClick() {
        // 起草正文，在线编辑word文档模板
        let organId = this.form.getFieldValue('organId')
        if (organId) {
          //根据机关待字获取绑定的excel在线访问路径
          let code = this.form.getFieldValue('code')
          let text = "official";
          getAction(this.url.getTemplateAndUser, { 'codeId': organId,"text":text,"code":code }).then((res) => {
            if (res.success) {
              this.draftLoading = true
              const params = {
                fileId: res.result.tempId,
                userId: res.result.userId,
                text:"official"
              }
              getAction(this.url.getViewUrlDbPath, params).then((res) => {
                if (res.msg === 'ok') {
                  let r = res.data
                  sessionStorage.wpsUrl = r.wpsUrl
                  sessionStorage.token = r.token
                  let routeUrl = this.$router.resolve({
                    path: '/modules/eoa/wps/viewFile'
                  })
                  this.text = true
                  window.open(routeUrl.href, '_blank')
                } else {
                  this.$message.warning('请求错误！')
                }
                this.draftLoading = false
              })
            }
          })
        } else {
          this.$message.warning('请选择机关待字')
        }

      },
      signature(){
        console.log(this.model.id)
        getAction(this.url.getSignature,{"id":this.model.id}).then((res) =>{
            if(res.success){
              this.draftLoading = true
              const params = {
                fileId: res.result.tempId,
                userId: res.result.userId,
                text:"official"
              }
              getAction(this.url.getViewUrlDbPath, params).then((res) => {
                if (res.msg === 'ok') {
                  let r = res.data
                  sessionStorage.wpsUrl = r.wpsUrl
                  sessionStorage.token = r.token
                  let routeUrl = this.$router.resolve({
                    path: '/modules/eoa/wps/viewFile'
                  })
                  this.text = true
                  window.open(routeUrl.href, '_blank')
                } else {
                  this.$message.warning('请求错误！')
                }
                this.draftLoading = false
              })
            }
        })
      },
      isDisabledAuth(code){
        return disabledAuthFilter(code,this.formData);
      },
    }
  }
</script>


<style >
  .inputHeight {
    width: 100%;
    border: none;
    border-radius: 0;
    background:#fff;
  }

  .textType {
    resize: none;
    height: 118px !important;
    font-size: 12px;
    border: 0 solid white;
    border-radius: 0;
    margin-bottom: 0;
  }
  .textareaType{
    width: 100%;
    height: 100%;
    position: relative;
    top: 22px;
    right: 6px;
    resize:none;
  }
  .title .ant-form-explain {
    margin-top:10px !important;
  }
  #staffCard {
    border: 1px solid #fff;
    box-shadow: 0 0 1px 1px #aaa, 3px 0 5px 0 #aaa, 0 4px 7px 0 #aaa;
    border-radius: 5px;
  }
  #staffLeaveTitle {
    margin-top: 1px;
    font-weight: 700;
    text-align: center;
    display: block;
    color: #000;
    font-size: 24px;
  }
  #staffEvectionTable {
    width: 100%;
    font-size: 12px;
    text-align: center;
    background-color: #fff;
    margin-top: 10px;
  }
  #staffEvectionTable .input .ant-input, #staffEvectionTable .text {
    border: 0 solid #000!important;
    border-radius: 0;
    background-color: #fff;
    margin: 0 auto;
    width: 100%;
    font-size: 12px;
    height: 38px;
  }
  #staffEvectionTable .ant-form-item {
    margin: 0;
  }
</style>